package com.hb.game.pk.ui.activity.game.erba;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hb.game.pk.R;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.NullBean;
import com.hb.game.pk.beans.XZBean;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.ui.activity.game.ErBaChatActivity;
import com.hb.game.pk.ui.activity.login.ForgetPwdActivity;
import com.hb.game.pk.utils.StringUtils;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ErBaXZActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.et_money_qz)
    EditText etMoneyQz;
    @BindView(R.id.btn_ok)
    Button btnOk;
    private String chang_id;
    private String room_id;
    private int time;
    private CountDownTimer timer;
    private boolean subLock=false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_erba_xz;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        Intent intent = getIntent();
        chang_id = intent.getStringExtra("chang_id");
        room_id = intent.getStringExtra("room_id");
        time = intent.getIntExtra("time", 0);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ErBaXZActivity.this.finish();
            }
        });

        tvCount.setText(String.format(getResources().getString(R.string.erba_xz), time));
        getTime(time);
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        getMineMoney();
    }

    private void getMineMoney() {
        Call<NullBean> getmoney = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).getmoney();
        getmoney.enqueue(new Callback<NullBean>() {
            @Override
            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                NullBean body = response.body();
                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    return;
                }

                if (!StringUtils.isEmpty(body.getData().toString())) {
                    tvMoney.setText(String.format(ErBaXZActivity.this.getResources().getString(R.string.erba_xs), (Float.parseFloat(body.getData().toString()) / 100f) + ""));
                } else {
                    tvMoney.setText(String.format(ErBaXZActivity.this.getResources().getString(R.string.erba_xs), "0"));
                }
            }

            @Override
            public void onFailure(Call<NullBean> call, Throwable t) {
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @OnClick(R.id.btn_ok)
    public void onViewClicked() {
        if (StringUtils.isEmpty(chang_id)) {
            ToastUtils.showToast("参数错误");
            return;
        }

        if (time == 0) {
            ToastUtils.showToast("下注时间已过");
            return;
        }

        String s = etMoneyQz.getText().toString();
        if (StringUtils.isEmpty(s)) {
            ToastUtils.showToast("请输入金额");
            return;
        }
        int i = Integer.parseInt(s);
        if (i < 10) {
            ToastUtils.showToast("下注金额必须大于10元");
            return;
        }

        if(subLock==true){
            return;
        }
        subLock=true;
        Call<XZBean> call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).erba_ble_money(room_id, s, chang_id);
        call.enqueue(new Callback<XZBean>() {
            @Override
            public void onResponse(Call<XZBean> call, Response<XZBean> response) {
                XZBean body = response.body();

                if (body == null) {
                    ToastUtils.showToast("请求错误");
                    subLock=false;
                    return;
                }
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(body.getInfo());
                    subLock=false;
                    return;
                }
                ToastUtils.showToast(body.getInfo());
                ErBaXZActivity.this.finish();
            }

            @Override
            public void onFailure(Call<XZBean> call, Throwable t) {
                subLock=false;
                ToastUtils.showToast("网络请求错误");
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

    private void getTime(int count_time) {
        timer = new CountDownTimer(count_time * 1000, 1000) {
            @Override
            public void onTick(long l) {
                time = Integer.parseInt(l / 1000 + "");
                tvCount.setText("");
                if (time == 1) {
                    time = 0;
                }
                tvCount.setText(String.format(getResources().getString(R.string.erba_xz), time));
            }

            @Override
            public void onFinish() {
            }
        };
        timer.start();
    }
}
